# Java 版本检查脚本
Write-Host "🔍 检查当前 Java 环境..." -ForegroundColor Green

Write-Host "`n📋 系统信息:" -ForegroundColor Yellow
Write-Host "操作系统: $([System.Environment]::OSVersion.VersionString)" -ForegroundColor Cyan

Write-Host "`n🔧 环境变量:" -ForegroundColor Yellow
Write-Host "JAVA_HOME: $env:JAVA_HOME" -ForegroundColor Cyan
Write-Host "PATH (Java相关): " -ForegroundColor Cyan
$env:Path -split ";" | Where-Object { $_ -like "*java*" -or $_ -like "*jdk*" } | ForEach-Object {
    Write-Host "  - $_" -ForegroundColor White
}

Write-Host "`n☕ Java 版本检查:" -ForegroundColor Yellow
try {
    $javaVersion = java -version 2>&1
    Write-Host $javaVersion -ForegroundColor White
    
    # 提取版本号
    $versionLine = $javaVersion | Select-String "openjdk version|java version" | Select-Object -First 1
    if ($versionLine -match '"(\d+)') {
        $majorVersion = $matches[1]
        Write-Host "`n当前 Java 主版本: $majorVersion" -ForegroundColor $(if ($majorVersion -eq "21") { "Green" } else { "Red" })
        
        if ($majorVersion -ne "21") {
            Write-Host "⚠️ 建议使用 Java 21 LTS 版本以获得最佳兼容性" -ForegroundColor Yellow
        } else {
            Write-Host "✅ 正在使用推荐的 Java 21 LTS 版本" -ForegroundColor Green
        }
    }
} catch {
    Write-Host "❌ 无法检测 Java 版本: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n🔨 Maven 版本检查:" -ForegroundColor Yellow
try {
    $mavenVersion = mvn -version 2>&1
    Write-Host $mavenVersion -ForegroundColor White
} catch {
    Write-Host "❌ 无法检测 Maven 版本: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n📂 已安装的 Java 版本:" -ForegroundColor Yellow
$javaInstallations = Get-ChildItem "C:\Program Files\Eclipse Adoptium\" -ErrorAction SilentlyContinue
if ($javaInstallations) {
    $javaInstallations | ForEach-Object {
        $isActive = $env:JAVA_HOME -like "*$($_.Name)*"
        $status = if ($isActive) { " (当前使用)" } else { "" }
        $color = if ($isActive) { "Green" } else { "Cyan" }
        Write-Host "  - $($_.Name)$status" -ForegroundColor $color
    }
} else {
    Write-Host "  未找到 Eclipse Adoptium Java 安装" -ForegroundColor Red
}

Write-Host "`n🎯 建议操作:" -ForegroundColor Yellow
if ($majorVersion -ne "21") {
    Write-Host "1. 运行 install-java21.ps1 安装 Java 21" -ForegroundColor White
    Write-Host "2. 运行 switch-to-java21.ps1 切换到 Java 21" -ForegroundColor White
    Write-Host "3. 重新启动 PowerShell 或 IDE" -ForegroundColor White
} else {
    Write-Host "✅ Java 环境配置正确，可以运行测试" -ForegroundColor Green
}

Write-Host "`n🧪 测试命令:" -ForegroundColor Yellow
Write-Host "运行单元测试: .\run-tests.ps1" -ForegroundColor Cyan
Write-Host "或者: mvn clean test" -ForegroundColor Cyan